<?php defined('RUN_KATANA') or die('Restricted access'); ?>
<?php echo $commander->getListCommandsPanel('n-sa-a-d-ad_types-ad_cats-ad_brands-sqlpager-sqlfilter-reload', ''); ?>
<?php 
$types  = $adc->getAdTypes();
$adcats = $adc->getAdCats();
$brands = $adc->getAdBrands();
?>
<div class="menu_list">
	<table style="clear:both;" id="menulist" class="item_list" cellspacing="0" cellpadding="0" border="0">
        <thead class="fields">
            <tr>
                <th class="first center">&nbsp;&nbsp;Id&nbsp;&nbsp;</th>
                <th class="nosort"><input title="<?php echo ucfirst($txt->getWord('all')); ?>" class="select_all" type="checkbox" name="select_all" value="" /></th>
                <th class="left nosort"><?php echo ucfirst($txt->getWord('type')); ?></th>
				<th class="left nosort"><?php echo ucfirst($txt->getWord('category')); ?></th>
				<th class="left nosort"><?php echo ucfirst($txt->getWord('brand')); ?></th>
				<th class="left nosort"><?php echo ucfirst($txt->getWord('user')); ?> Id</th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('title')); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('title').' (EN)'); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('clicks').' / '.$txt->getWord('list_views')); ?></th>
				<!--<th><?php echo ucfirst($txt->getWord('selling')); ?></th>-->
				<th class="nosort"><?php echo ucfirst($txt->getWord('buying')); ?></th>
				<!--<th><?php echo ucfirst($txt->getWord('used')); ?></th>-->
				<th class="nosort"><?php echo ucfirst($txt->getWord('new')); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('sold')); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('created')); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('edit')); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('active')); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('archive')); ?></th>
                <th class="last nosort">
					<?php if ($_GET['deleted'] > 0): ?>
					<?php echo ucfirst($txt->getWord('restore')); ?>
					<?php else: ?>
					<?php echo ucfirst($txt->getWord('delete')); ?>
					<?php endif; ?>
				</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>
<div id="add_dialog" title="<?php echo ucfirst($txt->getWord('new').' '.$txt->getWord('ad_category')); ?>?" style="display:none; text-align:left;">
	<p>
		<?php echo ucfirst($txt->getWord('type')); ?><br />
		<select class="medium" name="new_TypeId" id="new_TypeId" onchange="populateCats('new_TypeId', 'new_CatId');">
			<?php 
			echo '<option value="0">'.ucfirst($txt->getWord('choose').' '. $txt->getWord('type')).'</option>';
			if($adc->getAdTypes())
			{
				foreach($adc->getAdTypes() as $type)
				{ echo '<option value="'.$type->getId().'" '.$txt->getIsSelectedHTML($type->getId(), $_GET['typeId']) .'>'.$type->getTitle().'</option>'; }
			}
			?>
		</select><br />
		<?php echo ucfirst($txt->getWord('category')); ?><br />
		<select class="medium" name="new_CatId" id="new_CatId" onchange="populateBrands('new_TypeId', 'new_CatId', 'new_BrandId');">
			<?php 
			echo '<option value="0">'.ucfirst($txt->getWord('undefined')).'</option>';
			if($_GET['typeId'])
			{
				if($adc->getAdCats())
				{
					foreach($adc->getAdCats() as $cat)
					{ 
						if($cat->getTypeId() == $_GET['typeId'])
						{ echo '<option value="'.$cat->getId().'" '.$txt->getIsSelectedHTML($cat->getId(), $_GET['catId']) .'>'.$cat->getTitle().'</option>'; }
					}
				}
			}
			?>
		</select><br />
		<?php echo ucfirst($txt->getWord('brand')); ?><br />
		<select class="medium" name="new_BrandId" id="new_BrandId">
			<?php 
			echo '<option value="0">'.ucfirst($txt->getWord('undefined')).'</option>';
			?>
		</select><br /><br />
		<input type="radio" name="new_IsBuying" class="new_IsBuying" value="0" checked="checked" />
		<?php echo ucfirst($txt->getWord('selling')); ?>&nbsp;&nbsp;
		<input type="radio" name="new_IsBuying" class="new_IsBuying" value="1" />
		<?php echo ucfirst($txt->getWord('buying')); ?>
		<br /><br />
		<input type="radio" name="new_IsNew" class="new_IsNew" value="0" checked="checked" />
		<?php echo ucfirst($txt->getWord('used')); ?>&nbsp;&nbsp;
		<input type="radio" name="new_IsNew" class="new_IsNew" value="1" />
		<?php echo ucfirst($txt->getWord('new')); ?>
		<br />
		<br />
		<?php echo ucfirst($txt->getWord('title')); ?><br />
		<input class="medium" name="new_Title" type="text" value="" maxlength="80" id="new_Title" /><br />
		<?php echo ucfirst($txt->getWord('title')); ?> (EN)<br />
		<input class="medium" name="new_TitleEn" type="text" value="" maxlength="80" id="new_TitleEn" /><br />
	</p>
</div>
<div id="delete_dialog" title="<?php echo ucfirst($txt->getWord('delete').' '.$txt->getWord('ad_type')); ?>?" style="display:none; text-align:left;">
	<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span><?php echo ucfirst($txt->getWord('sure_you_want_to_delete')); ?></p>
</div>
<script type="text/javascript">

function populateCats(sourceId, targetId)
{
	jQuery.getJSON("json.php?con=<?php echo $_GET['con']; ?>&list=cat", { typeId: $("#"+sourceId).val(), ajax: 'true' }, function(j)
	{
		  var opts = '';
		  if(j)
		  {
			  for (var i = 0; i < j.length; i++) 
			  { opts += '<option value="' + j[i].id + '">' + j[i].title + '</option>'; }
		  }
		  else
		  { opts += '<option value="0"><?php echo ucfirst($txt->getWord('undefined')); ?></option>'; }
		  
		  jQuery("#"+targetId).html(opts);
		  populateBrands(targetId, '', 'new_BrandId');
	})
}

function populateBrands(sourceId, source2Id, targetId)
{
	jQuery.getJSON("json.php?con=<?php echo $_GET['con']; ?>&list=adbrand", { typeId: $("#"+sourceId).val(), catId: $("#"+source2Id).val(), ajax: 'true' }, function(j)
	{
		  var opts = '';
		  if(j)
		  {
			  for (var i = 0; i < j.length; i++) 
			  { opts += '<option value="' + j[i].id + '">' + j[i].title + '</option>'; }
		  }
		  else
		  { opts += '<option value="0"><?php echo ucfirst($txt->getWord('undefined')); ?></option>'; }
		  jQuery("#"+targetId).html(opts);
	})
}


function addNewDialog(itemId)
{
	jQuery('#add_dialog').dialog('open');
	jQuery('#add_dialog').dialog({
		bgiframe: true,
		resizable: false,
		height:360,
		modal: true,
		overlay: 
		{
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: 
		{
			'<?php echo ucfirst($txt->getWord('cancel')); ?>': function() 
			{
				jQuery(this).dialog('close');
			},
			'<?php echo ucfirst($txt->getWord('add')).' '.$txt->getWord('element'); ?>': function() 
			{
				jQuery.post("ajax.php?con=<?php echo $_GET['con']; ?>&list=<?php echo $_GET['list']; ?>", 
				{ 
					action:'<?php echo $commander->getPostAction(); ?>', 
					new_item_Title: jQuery('#new_Title').val(), 
					new_item_TitleEn: jQuery('#new_TitleEn').val(), 
					new_item_TypeId: jQuery('#new_TypeId').val(),
					new_item_CatId: jQuery('#new_CatId').val(),
					new_item_BrandId: jQuery('#new_BrandId').val(),
					new_item_IsBuying: jQuery('input:radio[name=new_IsBuying]:checked').val(),
					new_item_IsNew: jQuery('input:radio[name=new_IsNew]:checked').val()
				}).success(function() { loadItems(); });
				jQuery(this).dialog('close');
			}
		}
	});
}

function deleteDialog(itemId)
{
	jQuery('#delete_dialog').dialog('open');
	jQuery('#delete_dialog').dialog({
		bgiframe: true,
		resizable: false,
		height:140,
		modal: true,
		overlay: 
		{
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: 
		{
			'<?php echo ucfirst($txt->getWord('no')); ?>': function() 
			{
				jQuery(this).dialog('close');
			},
			'<?php echo ucfirst($txt->getWord('delete')).' '.$txt->getWord('ad_type'); ?>': function() 
			{
				jQuery.post("ajax.php?con=<?php echo $_GET['con']; ?>&list=<?php echo $_GET['list']; ?>", 
				{ 
					action:'<?php echo $commander->getPostAction(); ?>', 
					del_item_id: itemId} 
				).success(function() { loadItems(); });
				jQuery(this).dialog('close');
			}
		}
	});
}

<?php 
	$adc->loadAdsCount();
	$adc->Pager->calcPagesCount();
	
	$prev = 1;
	$next = 1;
	
	if($adc->Pager->getPagesCount() > $adc->Pager->getPageCurrent())
	{ $next = ($adc->Pager->getPageCurrent() + 1); }
	else
	{ $next = ($adc->Pager->getPageCurrent()); }
	
	if(($adc->Pager->getPageCurrent() > 1) && ($adc->Pager->getPagesCount() > $adc->Pager->getPageCurrent()))
	{ $prev = ($adc->Pager->getPageCurrent() - 1); }
?>

function loadItems()
{
	var typeId = <?php echo intval($_REQUEST['typeId']); ?>;
	var catId = <?php echo intval($_REQUEST['catId']); ?>;
	var brandId = <?php echo intval($_REQUEST['brandId']); ?>;
	var getUrl = 'ajax.php<?php echo $commander->getFormAction('ajax'); ?>';
	
	if($("#typeId").val() > 0)
	{ 
		typeId = $("#typeId").val();
		getUrl = getUrl + '&typeId='+typeId;
	}
	if($("#catId").val() > 0)
	{ 
		catId = $("#catId").val();
		getUrl = getUrl + '&catId='+catId;
	}
	if($("#brandId").val() > 0)
	{ 
		brandId = $("#brandId").val(); 
		getUrl = getUrl + '&brandId='+brandId;
	}
	getUrl = getUrl + '&amp;page=<?php echo $adc->Pager->getPageCurrent(); ?>&amp;q=<?php echo $_REQUEST['q']; ?>';
	
	jQuery('table.item_list colgroup').remove();
	jQuery('table.item_list tbody').empty();
	jQuery('table.item_list tbody').html('<?php echo $commander->getComponentListLoading(); ?>'); 
	
	jQuery.ajax(
	{
		url: getUrl,
		cache: false,
	  	success: function(html)
	  	{ 
			jQuery('table.item_list tbody').empty();
			jQuery('table.item_list tbody').empty();
			jQuery("table.item_list tbody").append(html); 
			/*
			jQuery("#menulist").addClass('tablesorter');
			jQuery("#menulist").tablesorter(
				{
					widthFixed: true,  
					headers: { 0: { sorter: 'numeric' }, 1: { sorter: false}, 12: {sorter: false}, 15: {sorter: false} }, 
					sortList: [[0,1]]
				});
			*/
		}
	});
}
</script>

